/*
 * CustomerAction.java
 * 
 * Created on 02/10/2007, 12:43:14 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.actions;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.Eleccion;
import model.bean.ListaCandidatos;
import model.bean.MiembroLista;
import model.bean.Socio;
import model.bean.Solicitud;

import model.factory.RecreacionalFactory;
import model.service.ListaCandidatosService;
import model.service.MiembroListaService;
import model.service.SocioService;
import model.service.SolicitudService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import view.common.Constants;
import view.common.struts.CustomActionForm;

import view.common.struts.CustomDispatchAction;

/**
 * @Archivo LoginAction.java
 * @Fecha                @Autor   @Cambios
 * 21/01/2011 02:35:29 PM       moshiro
 */
public class SolicitudAction extends CustomDispatchAction {

    private final static String PRINCIPAL = "principal";
    private final static String REGISTRAR = "registrar";
    private final static String REGISTRAR_LISTA_CANDIDATOS = "registrarListaCandidatos";
    private final static String MODIFICAR = "modificar";
    private final static String LISTAR = "listar";

    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Solicitud beanSearch = new Solicitud();
        if (req.getServletPath().indexOf("SolicitudInscripcion") != -1) {
            beanSearch.setTipo(Constants.SOL_INSCRIPCION);
        } else {
            beanSearch.setTipo(Constants.SOL_TACHA);
        }
        frm.setBeanSearch(beanSearch);
        frm.setBeanList(getSolicitudService().listarSolicitud(beanSearch));
        frm.setMapList(new HashMap());
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Solicitud beanSearch = (Solicitud) frm.getBeanSearch();
        frm.setBeanList(getSolicitudService().listarSolicitud(beanSearch));
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward refrescar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward retroceder(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return mapping.findForward(REGISTRAR);
    }

    public ActionForward registrar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        if (req.getParameter("codigoListaCandidato") == null) {
            Solicitud bean = new Solicitud();
            frm.setBean(bean);
        } else {
            Solicitud bean = (Solicitud) frm.getBean();
            if (bean == null) {
                bean = new Solicitud();
            }
            bean.getListaCandidatos().setCodigo(Long.parseLong(req.getParameter("codigoListaCandidato")));
            bean.getListaCandidatos().setNombre(req.getParameter("descripcionListaCandidato"));

        }
        ListaCandidatos filtro = new ListaCandidatos();
        filtro.setEstado(Constants.ESTADO_ACTIVO);
        frm.getMapList().put("candidatosList", getListaCandidatosService().listarListaCandidatos(filtro));
        frm.getMapList().put("sociosList", getSociosService().listarSocio(null));

        return mapping.findForward(REGISTRAR);
    }

    public ActionForward registrarListaCandidatos(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return mapping.findForward(REGISTRAR_LISTA_CANDIDATOS);
    }

    public ActionForward eliminar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        getSolicitudService().eliminarSolicitud(Long.valueOf(frm.getSelectedIndex()));
        return unspecified(mapping, form, req, resp);
    }

    public ActionForward modificar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Solicitud beanSelected = new Solicitud(Long.valueOf(frm.getSelectedIndex()));
        Solicitud bean = getSolicitudService().obtenerSolicitud(beanSelected);
        frm.setBean(bean);
        return mapping.findForward(MODIFICAR);
    }

    public ActionForward listar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Solicitud beanSelected = new Solicitud(Long.valueOf(frm.getSelectedIndex()));
        Solicitud bean = getSolicitudService().obtenerSolicitud(beanSelected);
        frm.setBean(bean);
        if (req.getServletPath().indexOf("SolicitudInscripcion") != -1) {
            MiembroLista filtro = new MiembroLista();
            filtro.setListaCandidatos(new ListaCandidatos(bean.getListaCandidatos().getCodigo()));
            frm.setFormFileList(getMiembroListaService().listarMiembroLista(filtro));
        }
        return mapping.findForward(LISTAR);
    }

    public ActionForward guardar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Solicitud bean = (Solicitud) frm.getBean();

        if (bean.getCodigo() == null) {
            if (req.getServletPath().indexOf("SolicitudInscripcion") != -1) {
                bean.setTipo(Constants.SOL_INSCRIPCION);
            } else {
                bean.setTipo(Constants.SOL_TACHA);
            }
            getSolicitudService().agregarSolicitud(bean);
        } else {
            //MODIFICAR
            getSolicitudService().modificarSolicitud(bean);
        }
        return unspecified(mapping, form, req, resp);
    }

    public SolicitudService getSolicitudService() {
        return RecreacionalFactory.getInstance().getSolicitudService();
    }

    public ListaCandidatosService getListaCandidatosService() {
        return RecreacionalFactory.getInstance().getListaCandidatosService();
    }

    public MiembroListaService getMiembroListaService() {
        return RecreacionalFactory.getInstance().getMiembroListaService();
    }

    public SocioService getSociosService() {
        return RecreacionalFactory.getInstance().getSocioService();
    }
}
